package com.example.app3.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.example.app3.R;

import java.util.ArrayList;
import java.util.List;

import static java.lang.String.valueOf;

public class RegisterActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        final EditText username = findViewById(R.id.editText);
        final EditText tel = findViewById(R.id.editText2);
        final EditText password = findViewById(R.id.editText4);
        final EditText password2 = findViewById(R.id.editText3);
        Button button2 = findViewById(R.id.button2);//注册
        Button button = findViewById(R.id.button);//重置

//      注册按钮
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判空
                if (!username.getText().toString().equals("") &&! password.getText().toString().equals("") &&! password2.getText().toString().equals("") &&! tel.getText().toString().equals("")) {

                    int tel1 = Integer.valueOf(tel.getText().toString());
                    String username1 = username.getText().toString();
                    String password1 = password.getText().toString();//密码
                    String password22 = password2.getText().toString();//确认密码

                    if (!username1.equals("") && !password1.equals("") && !password22.equals("") && !tel.getText().toString().equals("")) {

                        if (password1.equals(password22)) {


                            //登录（文件写入数据）

                            SharedPreferences sharedPreferences = getSharedPreferences("test", MODE_PRIVATE);

                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putString("username", username1);
                            editor.putString("password", password1);
                            editor.putInt("tel", tel1);
                            editor.commit();

                            Intent intent = new Intent(RegisterActivity.this, RegisterActivity.class);


                            intent.setClass(RegisterActivity.this, LoginActivity.class);
                            Toast.makeText(RegisterActivity.this, "注册成功，已登录", Toast.LENGTH_SHORT).show();

                            startActivity(intent);

                        } else {

                            Toast.makeText(RegisterActivity.this, "两次密码输入不一致", Toast.LENGTH_SHORT).show();
                            //警告框不一致
                        }
                    }

                }else {

                    Toast.makeText(RegisterActivity.this, "请输入完所有的信息再注册", Toast.LENGTH_SHORT).show();

                }


            }
        });

        //重置
        button.setOnClickListener(v->{
            username.setText("");
            password.setText("");
            tel.setText("");
            password2.setText("");
        });
    }
}